package com.bigseller.study;

import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.config.SslConfigs;

import java.util.Properties;
import java.util.concurrent.Future;

public class Test1 {
    /**
     * 创建支持SSL的Producer实例
     */
    public static Producer<String, String> createProducerWitchSSL() {
        /*Properties properties = new Properties();
        // 注意，这里的端口一定得是Kafka服务器上配置的SSL协议监听端口
        properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "124.70.208.68:8989");
        properties.setProperty(ProducerConfig.ACKS_CONFIG, "all");
        properties.setProperty(ProducerConfig.BATCH_SIZE_CONFIG, "12");
        properties.setProperty(ProducerConfig.LINGER_MS_CONFIG, "1");
        properties.setProperty(ProducerConfig.BUFFER_MEMORY_CONFIG, "1024");
        properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG
                , "org.apache.kafka.common.serialization.StringSerializer");
        properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG
                , "org.apache.kafka.common.serialization.StringSerializer");
        // 配置事务支持
        properties.setProperty(ProducerConfig.RETRIES_CONFIG, "1");
        properties.setProperty(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "trans-id");

        // SSL配置
       //properties.setProperty(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "SSL");
        properties.setProperty(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, "D:\\youdaoyun\\workspace_kafka\\kafka-study\\kafka-test\\src\\main\\resources\\client.truststore.jks");
        properties.setProperty(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, "charlie93321");
        properties.setProperty(SslConfigs.SSL_KEY_PASSWORD_CONFIG, "charlie93321");
        properties.setProperty("security.protocol", "SSL");
        properties.setProperty(SslConfigs.SSL_ENABLED_PROTOCOLS_CONFIG, "TLSv1.2");*/
        Properties producerProps = new Properties();

        producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "124.70.208.68:8989");

        producerProps.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SSL");

        producerProps.put(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, "D:\\youdaoyun\\workspace_kafka\\kafka-study\\kafka-test\\src\\main\\resources\\server.keystore.jks");
        producerProps.put(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, "charlie93321");
        producerProps.put(SslConfigs.SSL_KEY_PASSWORD_CONFIG, "charlie93321");

        producerProps.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, "D:\\youdaoyun\\workspace_kafka\\kafka-study\\kafka-test\\src\\main\\resources\\client.truststore.jks");
        producerProps.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, "charlie93321");

        producerProps.put(ProducerConfig.ACKS_CONFIG, "all");
        producerProps.put(ProducerConfig.RETRIES_CONFIG, 0);
        producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");

// 配置事务支持
        producerProps.setProperty(ProducerConfig.RETRIES_CONFIG, "1");
        producerProps.setProperty(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "trans-id");
        return new KafkaProducer<>(producerProps);
    }

    public static void main(String[] args) {
        producerAsyncSend();
    }

    /**
     * 发送消息
     */
    public static void producerAsyncSend() {
        String topicName = "MyTopic";
        String key = "test-key";
        String value = "this is test message!";

        Producer<String, String> producer = createProducerWitchSSL();
        // 初始化事务
        producer.initTransactions();
        try {
            // 开启事务
            producer.beginTransaction();

            // 构建消息对象
            ProducerRecord<String, String> record =
                    new ProducerRecord<>(topicName, key, value);
            // 发送一条消息
            Future<RecordMetadata> future = producer.send(record);
            System.out.println(future.get().timestamp());

            // 提交事务
            producer.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            // 发生异常回滚事务
            producer.abortTransaction();
        } finally {
            producer.close();
        }
    }
}
